Page({
  data: {
    min: 0,
    max: 100,
    count: 1,
    result: ''
  },

  onMinInput: function (e) {
    this.setData({
      min: parseInt(e.detail.value) || 0
    });
  },

  onMaxInput: function (e) {
    this.setData({
      max: parseInt(e.detail.value) || 100
    });
  },

  onCountInput: function (e) {
    this.setData({
      count: parseInt(e.detail.value) || 1
    });
  },

  draw: function () {
    const { min, max, count } = this.data;
    if (min >= max) {
      wx.showToast({
        title: '最小值必须小于最大值',
        icon: 'none'
      });
      return;
    }
    if (count < 1) {
      wx.showToast({
        title: '抽取个数必须大于0',
        icon: 'none'
      });
      return;
    }

    const resultSet = new Set();
    while (resultSet.size < count) {
      const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
      resultSet.add(randomNum);
    }

    this.setData({
      result: Array.from(resultSet).join(', ')
    });
  },

  copyResult: function () {
    const { result } = this.data;
    if (result) {
      wx.setClipboardData({
        data: result,
        success: function () {
          wx.showToast({
            title: '结果已复制',
            icon: 'success'
          });
        }
      });
    } else {
      wx.showToast({
        title: '没有结果可复制',
        icon: 'none'
      });
    }
  }
});